/* 
    数字格式化
    _checkTime(i:Number);
*/
function _checkTime(i) {
    return i < 10 && i >= 0 ? '0' + i : i;
};
/* 
  //严格类型判断
  $toClass(key:any);
*/
function $toClass(key) {
    return Object.prototype.toString.call(key).split(" ")[1].replace("]", "");
  }

function $formatDate(dateParams) {
    const dateType = $toClass(dateParams);

    let dateObj;
    if (dateType === "Number"
        || dateType === "String"
    ) {
        dateObj = new Date(dateParams);
    } else if (dateType === "Date") {
        dateObj = dateParams;
    } else {
        throw TypeError("the dateParams type must be [String,Number,Date]");
    }
    const year = _checkTime(dateObj.getFullYear());
    const month = _checkTime(dateObj.getMonth() + 1)
    const date = _checkTime(dateObj.getDate());
    const hours = _checkTime(dateObj.getHours());
    const minutes = _checkTime(dateObj.getMinutes());
    const seconds = _checkTime(dateObj.getSeconds());
    const milSeconds = dateObj.getMilliseconds();
    const format = `${year}-${month}-${date} ${hours}:${minutes}:${seconds}:${ milSeconds }`;
    return {
        year
        , month
        , date
        , hours
        , minutes
        , seconds
        , milSeconds
        , format
        , _date: dateObj
        , _timestamp: dateObj.getTime()
    };
}

module.exports = {
    $formatDate
}